package com.stgl.action;


import com.alibaba.fastjson.JSON;
import com.stgl.service.StudentService;
import com.stgl.util.*;
import net.sf.jxls.exception.ParsePropertyException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/student")
public class student {
    @Resource
    private StudentService studentService;



    @RequestMapping("Search_Student.html")
    @ResponseBody
    public Map<String,Object>Search_Student(HttpServletRequest request, HttpServletResponse response){
        String sname="";
        if(request.getParameter("sname")!=null){
            sname=request.getParameter("sname").toString().trim();
        }

        String sfzh="";
        if(request.getParameter("sfzh")!=null){
            sfzh=request.getParameter("sfzh").toString().trim();
        }

        String bmh="";
        if(request.getParameter("bmh")!=null){
            bmh=request.getParameter("bmh").toString().trim();
        }

        String hjszd="";
        if(request.getParameter("hjszd")!=null){
            hjszd=request.getParameter("hjszd").toString().trim();
        }

        String jzxm="";
        if(request.getParameter("jzxm")!=null){
            jzxm=request.getParameter("jzxm").toString().trim();
        }

        String jzsj="";
        if(request.getParameter("jzsj")!=null){
            jzsj=request.getParameter("jzsj").toString().trim();
        }

        String sfbmcg="";
        if(request.getParameter("sfbmcg")!=null){
            sfbmcg=request.getParameter("sfbmcg").toString().trim();
        }

        String bmsj="";
        if(request.getParameter("bmsj")!=null){
            bmsj=request.getParameter("bmsj").toString().trim();
        }

        String rxnj="";
        if(request.getParameter("rxnj")!=null){
            rxnj=request.getParameter("rxnj").toString().trim();
        }


        String ccsh="";
        if(request.getParameter("ccsh")!=null){
            ccsh=request.getParameter("ccsh").toString().trim();
        }

        String sbyy="";
        if(request.getParameter("sbyy")!=null){
            sbyy=request.getParameter("sbyy").toString().trim();
        }

        int page=1;
        if(request.getParameter("_PAGE_NO_")!=null){
            page=Integer.parseInt(request.getParameter("_PAGE_NO_").toString().trim());
        }

        int size=10;
        if(request.getParameter("_PAGE_SIZE_")!=null){
            size=Integer.parseInt(request.getParameter("_PAGE_SIZE_").toString().trim());
        }
        int start=page*size-size+1;
        int end=page*size;


        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sname",sname);
        gmap.put("bmh",bmh);
        gmap.put("sfzh",sfzh);
        gmap.put("hjszd",hjszd);
        gmap.put("jzxm",jzxm);
        gmap.put("jzsj",jzsj);
        gmap.put("sfbmcg",sfbmcg);
        gmap.put("bmsj",bmsj);
        gmap.put("rxnj",rxnj);
        gmap.put("ccsh",ccsh);
        gmap.put("sbyy",sbyy);

        gmap.put("start",start);
        gmap.put("end",end);
        String csid="";
        if(request.getParameter("csid")!=null && !request.getParameter("csid").toString().equals("")){
            csid=request.getParameter("csid").trim();
        }else{
            HttpSession session=((HttpServletRequest)request).getSession();
            if(session.getAttribute("csid")!=null){
                csid=session.getAttribute("csid").toString();
            }
        }

        gmap.put("csid",csid);
        List<Map<String,Object>>list1=studentService.Search_Student_page(gmap);
        int totals=studentService.Search_Student_count(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
      /*  double c = totals/size;
        int totalItems=(int)Math.ceil(c);*/
        rmap.put("totalItems",totals);
        return rmap;
    }




    /**学生报名导出**/
    @RequestMapping("/export_Student.html")
    @ResponseBody
    public void export_Student(HttpServletRequest request, HttpServletResponse res) throws ParsePropertyException, IOException, InvalidFormatException, IllegalAccessException {
        String sname="";
        if(request.getParameter("sname")!=null){
            sname=request.getParameter("sname").toString().trim();
        }

        String sfzh="";
        if(request.getParameter("sfzh")!=null){
            sfzh=request.getParameter("sfzh").toString().trim();
        }

        String bmh="";
        if(request.getParameter("bmh")!=null){
            bmh=request.getParameter("bmh").toString().trim();
        }

        String hjszd="";
        if(request.getParameter("hjszd")!=null){
            hjszd=request.getParameter("hjszd").toString().trim();
        }

        String jzxm="";
        if(request.getParameter("jzxm")!=null){
            jzxm=request.getParameter("jzxm").toString().trim();
        }

        String jzsj="";
        if(request.getParameter("jzsj")!=null){
            jzsj=request.getParameter("jzsj").toString().trim();
        }
        String sfbmcg="";
        if(request.getParameter("sfbmcg")!=null){
            sfbmcg=request.getParameter("sfbmcg").toString().trim();
        }
        String bmsj="";
        if(request.getParameter("bmsj")!=null){
            bmsj=request.getParameter("bmsj").toString().trim();
        }

        String rxnj="";
        if(request.getParameter("rxnj")!=null){
            rxnj=request.getParameter("rxnj").toString().trim();
        }

        String ccsh="";
        if(request.getParameter("ccsh")!=null){
            ccsh=request.getParameter("ccsh").toString().trim();
        }
        String sbyy="";
        if(request.getParameter("sbyy")!=null){
            sbyy=request.getParameter("sbyy").toString().trim();
        }

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sname",sname);
        gmap.put("sfzh",sfzh);
        gmap.put("bmh",bmh);
        gmap.put("hjszd",hjszd);
        gmap.put("jzxm",jzxm);
        gmap.put("jzsj",jzsj);
        gmap.put("sfbmcg",sfbmcg);
        gmap.put("bmsj",bmsj);
        gmap.put("rxnj",rxnj);
        gmap.put("ccsh",ccsh);
        gmap.put("sbyy",sbyy);
        String csid="";
        if(request.getParameter("csid")!=null && !request.getParameter("csid").toString().equals("")){
            csid=request.getParameter("csid").trim();
        }else{
            HttpSession session=((HttpServletRequest)request).getSession();
            if(session.getAttribute("csid")!=null){
                csid=session.getAttribute("csid").toString();
            }
        }

        gmap.put("csid",csid);
        List<Map<String,Object>>list1=studentService.Search_Student(gmap);

        Map<String,Object>resultm=new HashMap<String,Object>();
        resultm.put("list1",list1);
        /*****...................................获取数据end..............................******/
        String modalxls="exportstu.xlsx";

        ExportExcel.download(modalxls,resultm,request,res);

        return;
    }


    @RequestMapping("del_Student.html")
    @ResponseBody
    public Map<String,Object> del_Student(HttpServletRequest request, HttpServletResponse response){
        String sid=request.getParameter("sid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        Map<String,Object>getm=studentService.del_Student(gmap);
        return getm;
    }





    @RequestMapping("import_Student.html")
    @ResponseBody
    public Boolean import_Student(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map<String,Object>>lsitdata=getExceldatas(request);
        int x=0;
        for(int i=0;i<lsitdata.size();i++){
            Map<String,Object>maps=lsitdata.get(i);
            String bmh="";
            if(maps.get("报名号")!=null){
                bmh=maps.get("报名号").toString().trim();
            }else{
                continue;
            }

            Calendar cale = null;
            cale = Calendar.getInstance();
            int nowyear = cale.get(Calendar.YEAR);

            Map<String,Object>gmap=new HashMap<>();
            gmap.put("bmh",bmh);
            gmap.put("nowyear",nowyear);
            int m=studentService.update_Student(gmap);
            if(m!=3 && m>0){
                x++;
            }
        }
        if(x>0){
            return true;
        }else{
            return false;
        }
    }





    //获取数据
    public List<Map<String,Object>>getExceldatas(HttpServletRequest request) throws IOException {
        List<Map<String,Object>>listdata=new ArrayList<>();
        String filename=request.getParameter("filename").toString();
        String filepath=request.getParameter("filepath").toString();
        String realpath=request.getRealPath(filepath);

        //后去文件路径与文件后缀
        String suffix = filename.substring(filename.lastIndexOf(".") + 1);
        //获取文件流
        File srcFile = new File(realpath);
        //创建文件字节输入流对象，并接在源上
        InputStream is = new FileInputStream(srcFile);
        //获取表头
        String row_head="";
        String[] columns_arr =null;

        if (suffix.equals("xls")) {
            HSSFWorkbook wbs = new HSSFWorkbook(is);
            //获取第一个单元格
            HSSFSheet childSheet = wbs.getSheetAt(0);
            //获取第一行 第一行为表头
            HSSFRow rowone = childSheet.getRow(0);
            if (null != rowone) {
                for (int k = 0; k < rowone.getLastCellNum(); k++) {
                    HSSFCell cell = rowone.getCell(k);
                    if (null != cell) {
                        if (k > 0) {
                            row_head += ",";
                            row_head += ExcelUtils.getHSSFCellValue(cell);
                        } else {
                            row_head += ExcelUtils.getHSSFCellValue(cell);
                        }
                    }
                }
            }

            if(row_head.trim().equals("")){
                return listdata;
            }
            columns_arr = row_head.split(",");
            //数据从第二行开始
            for(int j=1;j<=childSheet.getLastRowNum();j++){
                HSSFRow row=childSheet.getRow(j);
                StringBuilder row_value=new StringBuilder();
                if(null != row){
                    Map<String, Object>data_row=new HashMap<String,Object>();
                    for(int k = 0;k<columns_arr.length;k++){
                        HSSFCell cell = row.getCell(k);
                        if(cell != null){
                            int celltype=cell.getCellType();
                            String keys=columns_arr[k];
                            String values="";
                            if(celltype==3){
                                continue;
                            }else{
                                cell.setCellType(cell.CELL_TYPE_STRING);
                                values=ExcelUtils.getHSSFCellValue(cell);
                            }
                            data_row.put(keys,values);
                            row_value.append(values);
                        }
                    }
                    if(row_value.length() > 0){
                        listdata.add(data_row);
                        row_value.delete(0, row_value.length()-1);
                    }
                }
            }
        } else {
            XSSFWorkbook wbs = new XSSFWorkbook(is);
            //第一个工作表
            XSSFSheet childSheet = wbs.getSheetAt(0);
            //第一行
            XSSFRow rowone = childSheet.getRow(0);
            if (null != rowone) {
                for (int k = 0; k < rowone.getLastCellNum(); k++) {
                    XSSFCell cell = rowone.getCell(k);
                    if (null != cell) {
                        int celltype=cell.getCellType();
                        if(celltype==3){
                            continue;
                        }
                        if (k > 0)
                            row_head += ",";
                        row_head += ExcelUtils.getXSSFCellValue(cell);
                    } else {
                        row_head += ExcelUtils.getXSSFCellValue(cell);
                    }
                }
            }
            if(row_head.trim().equals("")){
                return listdata;
            }
            columns_arr = row_head.split(",");
            //数据从第二行开始
            for(int j=1;j<=childSheet.getLastRowNum();j++){
                XSSFRow row=childSheet.getRow(j);
                StringBuilder row_value=new StringBuilder();
                if(null != row){
                    Map<String, Object>data_row=new HashMap<String,Object>();
                    for(int k = 0;k<columns_arr.length;k++){
                        XSSFCell cell = row.getCell(k);
                        if(cell != null){
                            int celltype=cell.getCellType();
                            String keys=columns_arr[k];
                            String values="";
                            if(celltype==3){
                                continue;
                            }else{
                                cell.setCellType(cell.CELL_TYPE_STRING);
                                values=ExcelUtils.getXSSFCellValue(cell);
                            }
                            data_row.put(keys,values);
                            row_value.append(values);
                        }
                    }
                    if(row_value.length() > 0){
                        listdata.add(data_row);
                        row_value.delete(0, row_value.length()-1);
                    }
                }
            }
        }
        return listdata;
    }



    @RequestMapping("notice_Student.html")
    @ResponseBody
    public Boolean notice_Student(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //修改报名结果
        String csid="";
        if(request.getParameter("csid")!=null && !request.getParameter("csid").toString().equals("")){
            csid=request.getParameter("csid").trim();
        }else{
            HttpSession session=((HttpServletRequest)request).getSession();
            if(session.getAttribute("csid")!=null){
                csid=session.getAttribute("csid").toString();
            }
        }

        Map<String,Object>gmap=new HashMap<String,Object>();
        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);
        gmap.put("csid",csid);
        gmap.put("nowyear",nowyear);
        boolean result=studentService.update_StudentJg(gmap);
        return result;
    }



    @RequestMapping("set_sfbmcg.html")
    @ResponseBody
    public Boolean set_sfbmcg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sid=request.getParameter("sid");
        String sfbmcg=request.getParameter("sfbmcg");
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        gmap.put("sfbmcg",sfbmcg);
        boolean result=studentService.set_sfbmcg(gmap);
        return result;
    }



    //初审
    @RequestMapping("ccsh_Student.html")
    @ResponseBody
    public Boolean ccsh_Student(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sid=request.getParameter("sid");
        String ccsh=request.getParameter("ccsh");
        String sbyy=request.getParameter("sbyy");
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("sid",sid);
        gmap.put("ccsh",ccsh);
        gmap.put("sbyy",sbyy);
        boolean result=studentService.ccsh_Student(gmap);
        return result;
    }






}
